package com.lanhang.controller;

import com.lanhang.controller.vo.SpecificationVO;
import com.lanhang.entity.Specification;
import com.lanhang.service.SpecificationService;
import com.lanhang.util.PageResult;
import com.lanhang.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/specification")
public class SpecificationController {
    @Autowired
    private SpecificationService specificationService;
    //要自己创一个service层业务层
    //1.获取一下规格列表
    @GetMapping("/list")
    public List<Specification> list(){
        return specificationService.findAll();
    }

    //2.分页查询规格信息
    @GetMapping("/findByPage")
    public PageResult<Specification> findByPage(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize){
        return specificationService.findByPage(page,pageSize);
    }

    //3.带分页的条件查询
    // @RequestBody : 将前端传入的json对象转换为java对象
    // @ResponseBody : 将java对象转换为json对象
    @PostMapping("/search")
    public PageResult<Specification> search(@RequestParam("page") Integer page,
                                      @RequestParam("pageSize") Integer pageSize,
                                      @RequestBody(required = false) SpecificationVO specificationVO){
        return specificationService.search(page,pageSize,specificationVO);
    }

    //4.把前端传过的规格数据保存起来

    @PostMapping("/save")
    public R save(@RequestBody Specification specification){
        return specificationService.save(specification) ? R.ok().message("添加成功！") : R.error().message("添加失败！");
    }

}
